package part3;

import java.util.Comparator;

/**
 * <p>Title: Lab #7 part 3</p>
 * <p>Description: CS 235 Lab #7 part 3
 *       Shows how to use the Comparator interface for sorting.</p>
 * @author Anthony Varghese
 * @date   September 5th 2014
 * @version 1.0
 */

class Data {
	private int   idata;
	private float rdata;
	private String sdata;
	public Data( int i, float r, String s){
		idata = i;
		rdata = r;
		sdata = s;
	}
	public Data( Data s){
		idata = s.idata;
		rdata = s.rdata;
		sdata = new String( s.sdata );
	}
	public String toString(){
		return "[ " + idata + ", " + rdata + ", " + sdata + "]";
	}
}

class CompareIntsInData {
	public int compare(Object o1, Object o2){
		return 0;
	}
}

class CompareFloatsInData {
	public int compare(Object o1, Object o2){
		return 0;
	}
}

class CompareStringsInData {
	public int compare(Object o1, Object o2){
		return 0;
	}
}

public class Part3 {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final int SIZE = 20;
		Data[] darr = new Data[SIZE];

		for (int i=0; i<SIZE; i++) {
			int   itemp = (int) (Math.random() * SIZE );
			float temp = (float) Math.random() * SIZE / 10.0f;
			String stemp = java.util.UUID.randomUUID().toString().replaceAll("[0-9]", "").replaceAll("-", "");
			darr[i] = new Data( itemp , temp, stemp );
		}
		
		System.out.println("Starting data: ");
		for (Data d : darr)
			System.out.println( d );
				
		// Sort the darr array in order of increasing values of the int data member
		sort( darr, null );
				
		System.out.println("\n\nSorted by first data member: ");
		for (Data d : darr)
			System.out.println( d );

		// Sort the darr array in order of increasing values of the float data member
		sort( darr, null );
						
		System.out.println("\n\nSorted by second data member: ");
		for (Data d : darr)
			System.out.println( d );

		// Sort the darr array in order of increasing values of the String data member
		sort( darr, null );
								
		System.out.println("\n\nSorted by third data member: ");
		for (Data d : darr)
			System.out.println( d );
	}

	/**
	 * sort - sort the input array d - not working yet
	 * 
	 * This method uses the Selection sort algorithm to sort
	 * 
	 * Preconditions:  none of the elements of d are nulls and each is an object of type Data
	 * Postconditions: the elements of the array d will be in sorted order so that 
	 *     d[i].idata <= d[i+1].idata for i from 0 to d.length 
	 * @param d - array of Data elements
	 */
	private static void sort(Data[] d, Comparator c) {
		final int N = d.length;
		if (N <= 1)
			return;

		for (int i=0; i<N; i++) {
			int minIndex = i;

			/*
			 * Find the next smallest item in d
			 */
			for (int j=i+1; j<N; j++)
//				if (c.compare( d[j], d[minIndex] ) < 0 )
					minIndex = j;

			if (minIndex != i) { //swap
				Data temp = d[i];
				d[i] = d[minIndex];
				d[minIndex] = temp;
			}
		}
	}
}